#include "audio_encoder.h"
#include <speex/speex.h>
#include "adpcm-pcm.h"

Audio_Encode_t audio_encode = {0};

//uint16_t task_id_audio_encode = TASK_ID_NONE;
//uint16_t task_id_audio_encode1 = TASK_ID_NONE;


void *encoder_state = NULL;
// Initialization of the structure that holds the bits
static SpeexBits bits;

static ima_adpcm_state_t* state = NULL;

// static uint8_t data[AUDIO_ENCODE_FRAME_LEN] = {0};
// static uint8_t encode_data[AUDIO_ENCODE_FRAME_LEN] = {0};
//extern void notify_data(uint8_t *data,uint8_t len);
void audio_encoder_init(void)
{
	audio_encode.status = ENCODER_STATE_IDLE;
	//audio_encode.pcm_in_ringbuffer = y_ringbuf_create(ENCODE_RINGBUFFER_LEN);
	//audio_encode.encoder_out_ringbuffer = y_ringbuf_create(ENCODE_RINGBUFFER_LEN);
	
	//task_id_audio_encode = os_task_create(audio_encode_task);
	//printf("task_id_audio_encode %d\r\n",task_id_audio_encode);

	
//printf("heap size %d\r\n",os_get_free_heap_size());
////encoder_state = speex_encoder_init(&speex_nb_mode);
//printf("heap size %d\r\n",os_get_free_heap_size());
	
// Set quality to 8 (15 kbps)
		#if 1

	
			uint32_t frame_size= 0;
				
				// 设置编码器和解码器参数
				int sample_rate = 8000;
				int quality = 4;

					encoder_state = speex_encoder_init(&speex_nb_mode);
					speex_encoder_ctl(encoder_state, SPEEX_SET_SAMPLING_RATE, &sample_rate);
					speex_encoder_ctl(encoder_state,SPEEX_GET_FRAME_SIZE,&frame_size);
					speex_encoder_ctl(encoder_state, SPEEX_SET_QUALITY, &quality);
					printf("frame_size =%d\r\n",frame_size);
					speex_bits_init(&bits);
		#else
		
			/*Init adpcm*/
		state = ima_adpcm_init(state, IMA_ADPCM_DVI4);
		#endif
}



void audio_speex_encode(uint8_t *in,uint8_t *out,uint16_t *len)
{
        //printf("%s\r\n",__FUNCTION__);
		speex_bits_reset(&bits);
        speex_encode_int(encoder_state, (short *)in, &bits);
		//speex_bits_nbytes(&bits);
        // Copy the bits to an array of char that can be written
        size_t s = speex_bits_write(&bits, (char *)out, 64);

		*len = s;
		//printf("encode size %d\r\n",s);
}



void audio_adpcm_encode(uint8_t *in,uint16_t _in_len,uint8_t *out,uint16_t *out_len)
{

	int len = ima_adpcm_encode(state, out,(uint16_t*)in, _in_len/2);
	*out_len = len;
        //printf("%s\r\n",__FUNCTION__);
		//printf("encode size %d\r\n",s);
}



 __attribute__((section("ram_code"))) void audio_encoder_start(void)
{
	//printf("audio_encoder_start\r\n");
	// uint16_t buffer_size = 0,act_len = 0;
	// memset(data,0,sizeof(data));
	// memset(data,0,sizeof(encode_data));
	// //buffer_size = get_audio_encoder_valid_size();
	// //if(buffer_size>=AUDIO_ENCODE_FRAME_LEN)
	// {
	// 		audio_encode.status = ENCODER_STATE_BUSY;
	// 	//printf("1 heap size %d\r\n",os_get_free_heap_size());
	// 		//speex_bits_reset(&bits);
	// 		act_len = y_ringbuf_read_clear(audio_encode.pcm_in_ringbuffer,data,AUDIO_ENCODE_FRAME_LEN);
		
	// 		//Encode frame
	// 	#if 0
    //     speex_encode_int(encoder_state, (short *)data, &bits);
	// 			//speex_bits_nbytes(&bits);
    //     // Copy the bits to an array of char that can be written
    //     size_t s = speex_bits_write(&bits, (char *)encode_data, AUDIO_ENCODE_FRAME_LEN);
	// 			printf("2 heap size %d\r\n",os_get_free_heap_size());	
	// 			printf("src size %d,encode size %d\r\n",act_len,s);
	// 	#else
		
	// 	int len = ima_adpcm_encode(state, encode_data,(uint16_t*)data, act_len);
	// 	printf("Encode data length is:%d\r\n", len);
	
		
	// 	#endif
	// 		audio_encode.status = ENCODER_STATE_IDLE;
	// }
	
}

void audio_encoder_stop(void)
{
	
}

 __attribute__((section("ram_code"))) bool audio_encoder_src_input(uint8_t*data,uint16_t len)
{
		return y_ringbuf_write(audio_encode.pcm_in_ringbuffer,data, len);
}

 __attribute__((section("ram_code"))) uint16_t get_audio_encoder_valid_size()
{
	return y_ringbuf_get_used_size(audio_encode.pcm_in_ringbuffer);
}



 __attribute__((section("ram_code"))) bool get_audio_encoder_is_busy(void)
{
		if(audio_encode.status == ENCODER_STATE_BUSY)
			return true;
		else
			return false;
}




#if 0
 __attribute__((section("ram_code"))) void msg_post_encode_handler()
{
	//printf("%s\r\n",__FUNCTION__);
	os_event_t event={0};
	event.event_id = ENCODER_EVENT;
	event.src_task_id = TASK_ID_NONE;
	event.param = NULL;
	event.param_len = 0;
	os_msg_post(task_id_audio_encode,&event);
}


#endif


